package com.lxw.station.chatgpt;

import com.lxw.station.service.RestTemplateService;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

public class HelloChatGPT {

    private static final String API_KEY = "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"; // Replace with your actual OpenAI API key
    private static final String API_URL = "https://api.openai.com/v1/chat/completions";
    private static RestTemplate restTemplate = new RestTemplate();

//    static {
//        HttpHost proxy = new HttpHost("localhost", 7890);
//        DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
//        CloseableHttpClient httpClient = HttpClients.custom()
//                .setRoutePlanner(routePlanner)
//                .build();
//
//        HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient);
//
//        restTemplate = new RestTemplate(factory);
//    }

    public static void main(String[] args) {
//        extracted();
//        extracted1();
//        getModelList();
        save();


    }


    public static void save() {
        String curl = "curl 'https://github.com/search?q=%E6%AF%95%E4%B8%9A%E8%AE%BA%E6%96%87%E6%A8%A1%E7%89%88&type=repositories' \\\n" +
                "  -H 'authority: github.com' \\\n" +
                "  -H 'accept: application/json' \\\n" +
                "  -H 'accept-language: zh-CN,zh;q=0.9' \\\n" +
                "  -H 'cookie: _octo=GH1.1.1506194046.1707203753; _device_id=1234364b687038ea5e0803df62df47af; saved_user_sessions=40336107%3AJrUaVDhwKW78FsuP9wjaveORIqOtIuh51U6trY92U7S73fUU; user_session=JrUaVDhwKW78FsuP9wjaveORIqOtIuh51U6trY92U7S73fUU; __Host-user_session_same_site=JrUaVDhwKW78FsuP9wjaveORIqOtIuh51U6trY92U7S73fUU; logged_in=yes; dotcom_user=lxw626; color_mode=%7B%22color_mode%22%3A%22auto%22%2C%22light_theme%22%3A%7B%22name%22%3A%22light%22%2C%22color_mode%22%3A%22light%22%7D%2C%22dark_theme%22%3A%7B%22name%22%3A%22dark%22%2C%22color_mode%22%3A%22dark%22%7D%7D; preferred_color_mode=light; tz=Asia%2FShanghai; _gh_sess=ogspX0OP3W7r7cM%2FsewiDIpF8inH0IFgxdgaMfnezKnuqvnlmKEjGeHrLS9pFz7hC%2FiTeM5EmSokIsy7t8FC9ZnOwQr9dfH776i4I9A2yBSugNyx8vlf8TWQzRyd%2F8SgZX7AgRY1NV%2FXs00089%2Bxv8F4afdi1dbDJPUEAgvngWxLj6VDSaD7FmsrzH%2B1rMXEwetwT6fw%2Fwg%2Fy1Sg%2FZWN0jSZjUcUs5onP%2BpR46G43RbLZuk2JTXfDhpyXSM57zaB0RBGZDL%2FQlsfQz%2Bx04xdRxtcFNUWeD5XMEVis2Scne%2BivknCnA2uEaYrgFdfoCzQyTnh3xSQmuE7Plw3ewETR5RX7BBVxoZW1TD%2B%2BvuanqoCGSK9AgRIHyAQB4DKHu1v--Jv6P2qEBOGZiUisa--zyK7X8cqloxf5R%2F2YEfPzA%3D%3D' \\\n" +
                "  -H 'if-none-match: W/\"c993669f3f93b1b72bd66f8940f3713e\"' \\\n" +
                "  -H 'referer: https://github.com/search?q=%E6%AF%95%E4%B8%9A%E8%AE%BA%E6%96%87%E6%A8%A1%E7%89%88&type=repositories' \\\n" +
                "  -H 'sec-ch-ua: \"Chromium\";v=\"122\", \"Not(A:Brand\";v=\"24\", \"Google Chrome\";v=\"122\"' \\\n" +
                "  -H 'sec-ch-ua-mobile: ?0' \\\n" +
                "  -H 'sec-ch-ua-platform: \"Windows\"' \\\n" +
                "  -H 'sec-fetch-dest: empty' \\\n" +
                "  -H 'sec-fetch-mode: cors' \\\n" +
                "  -H 'sec-fetch-site: same-origin' \\\n" +
                "  -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36' \\\n" +
                "  -H 'x-github-target: dotcom' \\\n" +
                "  -H 'x-requested-with: XMLHttpRequest'";


        String url = "https://github.com/search?q=%E6%AF%95%E4%B8%9A%E8%AE%BA%E6%96%87%E6%A8%A1%E7%89%88&type=repositories&p=1";
        HttpHeaders httpHeaders = RestTemplateService.getHttpHeaders(curl);
        HttpEntity<Object> httpEntity = new HttpEntity<>(null, httpHeaders);
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        System.out.println(exchange.getBody());
//        githubProjectService.save(githubProject);
    }


    private static void extracted1() {
//        String url = "https://www.google.com";
        String url = "https://api.openai.com";
        try {
            String result = restTemplate.getForObject(url, String.class);
            System.out.println("=======Result: " + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void extracted() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(API_KEY);

        String userMessage = "推荐几部好看的电影";
        // gpt-3.5-turbo
        String requestBody = "{\n" +
                "  \"model\": \"gpt-3.5-turbo\",\n" +
                "  \"messages\": [\n" +
                "    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n" +
                "    {\"role\": \"user\", \"content\": \"" + userMessage + "\"}\n" +
                "  ]\n" +
                "}";

        HttpEntity<String> request = new HttpEntity<>(requestBody, headers);


        ResponseEntity<String> response = restTemplate.exchange(
                API_URL,
                HttpMethod.POST,
                request,
                String.class
        );

        String body = response.getBody();
        System.out.println("body = " + body);
    }


}
